package com.hungryinlindholmen;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * This class supplies the restaurant page with info from the database. The
 * class takes values from the search form and makes queries with them.
 * 
 * @author Olof Ekelund.
 */

public class RestService {
    ResultSet rs;
    String restaurant;
    Connection con;
    ResultSet carteRS;
    ResultSet lunchRS;

    public RestService() {

    }

    public RestService(String rest_id, Connection con) {
	restaurant = rest_id;

	this.con = con;

	if (con == null) {
	    try {
		Class.forName("com.mysql.jdbc.Driver");
	    } catch (ClassNotFoundException e) {
		System.out.println(e);
	    }
	    try {
		con = DriverManager.getConnection(
			"jdbc:mysql://db.student.chalmers.se/", "olofek",
			"Ac6Jkdoa");
	    } catch (Exception e) {
		System.out.println(e);
	    }
	}
	try {
	    // query to get restaurant info
	    Statement stmt = con.createStatement();

	    String query = "SELECT * " + "FROM olofek.restaurant AS rest "
		    + "WHERE rest_id = '" + rest_id + "'";

	    rs = stmt.executeQuery(query);
	} catch (Exception e) {
	    System.out.println(e);
	}

	try {
	    // query to get a la carte menuitems.

	    Statement stmt = con.createStatement();

	    String query = "SELECT dish, description, price "
		    + "FROM olofek.menu AS rest " + "WHERE rest_id = '"
		    + rest_id + "'";

	    carteRS = stmt.executeQuery(query);
	} catch (Exception e) {
	    System.out.println(e);
	}

	try {
	    // query to get lunch menuitems.

	    Statement stmt = con.createStatement();

	    String query = "SELECT Date, dish, description, price "
		    + "FROM olofek.lunch AS rest " + "WHERE rest_id = '"
		    + rest_id + "'";

	    lunchRS = stmt.executeQuery(query);
	} catch (Exception e) {
	    System.out.println(e);
	}
    }

    public void updateClicks(String dish_id) {

	try {
	    Statement stmt = con.createStatement();

	    String query = "UPDATE olofek.stats SET rest_clicks = (rest_clicks + 1) WHERE rest_id = '"
		    + restaurant + "';";

	    stmt.executeUpdate(query);
	} catch (Exception e) {
	    System.out.println(e);
	}

	if (!(dish_id.equals("null"))) {
	    try {
		Statement stmt = con.createStatement();

		String query = "UPDATE olofek.stats SET dish_clicks = (dish_clicks + 1) WHERE dish = '"
			+ dish_id + "';";

		stmt.executeUpdate(query);
	    } catch (Exception e) {
		System.out.println(e);
	    }

	    try {
		Statement stmt = con.createStatement();

		String query = "UPDATE olofek.stats SET dish_lunch_clicks = (dish_lunch_clicks + 1) WHERE dish_lunch = '"
			+ dish_id + "';";

		stmt.executeUpdate(query);
	    } catch (Exception e) {
		System.out.println(e);
	    }
	}
    }

    public ResultSet getRs() {
	return rs;
    }

    public String getRestaurant() {
	return restaurant;
    }

    public void setRestaurant(String restaurant) {
	this.restaurant = restaurant;
    }

    public ResultSet getCarteRS() {
	return carteRS;
    }

    public void setCarteRS(ResultSet carteRS) {
	this.carteRS = carteRS;
    }

    public ResultSet getLunchRS() {
	return lunchRS;
    }

    public void setLunchRS(ResultSet lunchRS) {
	this.lunchRS = lunchRS;
    }
}
